﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.IO;
using System.Windows.Controls;

namespace jacDigitalDrum
{
    public partial class Window1 : Window
    {

        void bt_del_Click(object sender, RoutedEventArgs e)
        {
            if (lb_list.SelectedItems.Count == 1)
            {
                var f = ((ListBoxItem)lb_list.SelectedItem).Tag as jacData;
                string p = @"d:\\jacDrum\\" + f.Name;
                Directory.Delete(p, true);
                Window1_Loaded(this, new RoutedEventArgs());
            }
        }

        void bt_refresh_Click(object sender, RoutedEventArgs e)
        {
            Window1_Loaded(this, new RoutedEventArgs());
        }

        void lb_list_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (lb_list.SelectedItems.Count == 1)
            {
                var f = ((ListBoxItem)lb_list.SelectedItem).Tag as jacData;
                string p = @"d:\\jacDrum\\" + f.Name + "\\setting.xml";
                if (File.Exists(p))
                {
                    var jd = ObjectXmlSerializer.LoadFromXml<jacData>(p);

                    sounds[0] = LoadSoundFile(jd.a0);
                    sounds[1] = LoadSoundFile(jd.a1);
                    sounds[2] = LoadSoundFile(jd.a2);
                    sounds[3] = LoadSoundFile(jd.a3);
                    sounds[4] = LoadSoundFile(jd.a4);
                    sounds[5] = LoadSoundFile(jd.a5);

                    s1.tbk_sound.Text = s1.tbk_sound.Text = Path.GetFileName(jd.a0);
                    s2.tbk_sound.Text = s2.tbk_sound.Text = Path.GetFileName(jd.a1);
                    s3.tbk_sound.Text = s3.tbk_sound.Text = Path.GetFileName(jd.a2);
                    s4.tbk_sound.Text = s4.tbk_sound.Text = Path.GetFileName(jd.a3);
                    s5.tbk_sound.Text = s5.tbk_sound.Text = Path.GetFileName(jd.a4);
                    s6.tbk_sound.Text = s6.tbk_sound.Text = Path.GetFileName(jd.a5);

                    s1.soundPath = jd.a0;
                    s2.soundPath = jd.a1;
                    s3.soundPath = jd.a2;
                    s4.soundPath = jd.a3;
                    s5.soundPath = jd.a4;
                    s6.soundPath = jd.a5;
                }
                else
                {
                    MessageBox.Show("不是合法的鼓组文件");
                }
            }
        }

        void Window1_Loaded(object sender, RoutedEventArgs e)
        {
            lb_list.Items.Clear();
            string p = @"d:\\jacDrum";
            if (Directory.Exists(p))
            {
                var dirs = Directory.GetFileSystemEntries(p);
                foreach (var d in dirs)
                {
                    if (File.Exists(d + "\\setting.xml"))
                    {
                        var jd = ObjectXmlSerializer.LoadFromXml<jacData>(d + "\\setting.xml");
                        ListBoxItem lb = new ListBoxItem();
                        lb.Content = jd.Name;
                        lb.Tag = jd;
                        lb_list.Items.Add(lb);
                    }
                }
            }

            for (int i = 0; i <= 5; i++)
            {
                sounds.Add("k");
            }

            if (lb_list.Items.Count > 0)
            {
                lb_list.SelectedIndex = 0;
            }
        }

        List<Jitem> js = new List<Jitem>();

        void bt_edit_Click(object sender, RoutedEventArgs e)
        {
            if (!string.IsNullOrEmpty(tb_Name.Text))
            {
                js.Add(n1);
                js.Add(n2);
                js.Add(n3);
                js.Add(n4);
                js.Add(n5);
                js.Add(n6);

                if (!checkNul(js) & !checkWavs(js))
                {
                    string p = @"d:\\jacDrum\\" + tb_Name.Text;
                    if (!Directory.Exists(p))
                    {
                        Directory.CreateDirectory(p);
                    }

                    string a = p + "\\" + Path.GetFileName(n1.sound);
                    string b = p + "\\" + Path.GetFileName(n2.sound);
                    string c = p + "\\" + Path.GetFileName(n3.sound);
                    string d = p + "\\" + Path.GetFileName(n4.sound);
                    string f = p + "\\" + Path.GetFileName(n5.sound);
                    string g = p + "\\" + Path.GetFileName(n6.sound);

                    savefile(n1.sound, a);
                    savefile(n2.sound, b);
                    savefile(n3.sound, c);
                    savefile(n4.sound, d);
                    savefile(n5.sound, f);
                    savefile(n6.sound, g);

                    jacData jd = new jacData();
                    jd.Name = tb_Name.Text;
                    jd.model = cbo_model.SelectionBoxItem.ToString();
                    jd.a0 = a;
                    jd.a1 = b;
                    jd.a2 = c;
                    jd.a3 = d;
                    jd.a4 = f;
                    jd.a5 = g;

                    ObjectXmlSerializer.SaveToXml<jacData>(p + "\\setting.xml", jd);

                    MessageBox.Show("ok");
                }
                else
                {
                    MessageBox.Show("有未定义或相同名字的音频文件!");
                }
            }
        }

        bool checkWavs(List<Jitem> items)
        {
            bool iture = false;
            for (int i = 0; i <= 5; i++)
            {
                foreach (var d in items)
                {
                    if (d.Name != items[i].Name)
                    {
                        if (Path.GetFileName(items[i].sound) == Path.GetFileName(d.sound))
                        {
                            iture = true;
                            break;
                        }
                    }
                }
                if (iture)
                {
                    break;
                }
            }
            return iture;
        }

        bool checkNul(List<Jitem> items)
        {
            bool istrue = false;

            foreach (var d in items)
            {
                if (d.sound == null)
                {
                    istrue = true;
                    break;
                }
            }
            return istrue;
        }

        bool savefile(string fromfile, string tofile)
        {
            if (!File.Exists(tofile))
            {
                File.Copy(fromfile, tofile, false);
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
